package com.autumn.platform.web.validation;

import java.text.DateFormat;
import java.text.SimpleDateFormat;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

public class DateValidator implements ConstraintValidator<Date, String> {

    private DateFormat format;

    @Override
    public void initialize(Date constraintAnnotation) {
        this.format = new SimpleDateFormat(constraintAnnotation.format());
    }

    @Override
    public boolean isValid(String value, ConstraintValidatorContext context) {
        if (null != value) {
            try {
                this.format.parse(value);
            } catch (Exception e) {
                return false;
            }
        }
        return true;
    }
}
